/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BotFrame.java
 *
 * Created on Nov 29, 2009, 11:24:44 AM
 */
package com.rs247.ui;

import com.rs247.client.*;
import com.rs247.input.Keyboard;
import com.rs247.script.ScriptHandler;
import com.rs247.script.threads.*;
import com.rs247.util.Echo;
import java.applet.Applet;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import javax.swing.JScrollPane;

/**
 *
 * @author Michael
 */
public class BotFrame extends javax.swing.JFrame {

    public static ArrayList<Reflection> hooks = new ArrayList<Reflection>();
    private int status = 0;
    private Applet a;
    public Echo echo;
    public Graphics g;
    private ScriptHandler scriptHandler;

    /** Creates new form BotFrame */
    public BotFrame() {

        initComponents();
        this.setSize(766, 600);
        this.setLocationRelativeTo(null);
        console = new Console(consolePane);
        consolePane.setViewportView(console);
        System.setErr(console.getErrStream());
        System.setIn(console.getInStream());
        System.setOut(console.getOutStream());
        echo = new Echo(this);
        rsLoader = new RuneClassLoader(loadSigned.getState());



    }

    public Echo getEcho() {
        return this.echo;
    }

    public Console getConsole() {
        return this.console;
    }

    public JScrollPane getClientPane() {
        return this.clientPane;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        clientPane = new javax.swing.JScrollPane();
        consolePane = new javax.swing.JScrollPane();
        jButton1 = new javax.swing.JButton();
        menu = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        mnScripts = new javax.swing.JMenu();
        loadScripts = new javax.swing.JMenuItem();
        mnBotting = new javax.swing.JMenu();
        loadRS = new javax.swing.JMenuItem();
        mnSettings = new javax.swing.JMenu();
        loadSigned = new javax.swing.JCheckBoxMenuItem();
        rRandoms = new javax.swing.JCheckBoxMenuItem();
        mnDebug = new javax.swing.JMenu();
        loginIndex = new javax.swing.JMenuItem();
        doDraw = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("rs24-7");

        jButton1.setText("Start CanvasPainter");
        jButton1.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jMenu1.setText("File");

        jMenuItem1.setText("About");
        jMenu1.add(jMenuItem1);

        mnScripts.setText("Scripts");

        loadScripts.setText("Load Scripts");
        loadScripts.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadScriptsActionPerformed(evt);
            }
        });
        mnScripts.add(loadScripts);

        jMenu1.add(mnScripts);

        menu.add(jMenu1);

        mnBotting.setText("Botting");

        loadRS.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_R, java.awt.event.InputEvent.CTRL_MASK));
        loadRS.setText("Load RuneScape");
        loadRS.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadRSActionPerformed(evt);
            }
        });
        mnBotting.add(loadRS);

        menu.add(mnBotting);

        mnSettings.setText("Settings");

        loadSigned.setText("Load Signed");
        loadSigned.addChangeListener(new javax.swing.event.ChangeListener() {

            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                loadSignedStateChanged(evt);
            }
        });
        mnSettings.add(loadSigned);

        rRandoms.setSelected(true);
        rRandoms.setText("Run Randoms");
        mnSettings.add(rRandoms);

        menu.add(mnSettings);

        mnDebug.setText("Debug");

        loginIndex.setText("Get Login Index");
        loginIndex.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginIndexActionPerformed(evt);
            }
        });
        mnDebug.add(loginIndex);

        doDraw.setText("Load Canvas");
        doDraw.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                doDrawActionPerformed(evt);
            }
        });
        mnDebug.add(doDraw);

        menu.add(mnDebug);

        setJMenuBar(menu);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(consolePane, javax.swing.GroupLayout.DEFAULT_SIZE, 758, Short.MAX_VALUE).addComponent(clientPane, javax.swing.GroupLayout.DEFAULT_SIZE, 758, Short.MAX_VALUE).addGroup(layout.createSequentialGroup().addComponent(jButton1).addContainerGap()));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addComponent(clientPane, javax.swing.GroupLayout.PREFERRED_SIZE, 416, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(consolePane, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jButton1).addContainerGap(14, Short.MAX_VALUE)));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void loadRSActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadRSActionPerformed
        if (status == 0) {
            a = new RuneStub(loadSigned.getState(), 99, rsLoader).getApplet();
            a.init();
            a.start();
            clientPane.setViewportView(a);

            try {
                this.loadAllHooks();
            } catch (Exception e) {
                System.err.println("Error getting Hooks");
            }


            Startup isl = new Startup(this);
            Thread th = new Thread(isl);
            th.start();

        } else {
            a.stop();
            a.destroy();
            clientPane.setViewportView(null);
            loadRS.setText("Load RuneScape");
            a = null;
            status = 0;
        }
    }//GEN-LAST:event_loadRSActionPerformed

    private void loginIndexActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginIndexActionPerformed
    }//GEN-LAST:event_loginIndexActionPerformed

    private void doDrawActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doDrawActionPerformed
        try {
            Canvas c = (Canvas) this.getObjectValue("getCanvas()").getValue();
            if (c != null) {
                System.out.println("Not null canvas");
            } else {
                System.out.println("Canvas Null");
            }
            Graphics g = c.getGraphics();
            g.setColor(Color.white);
            g.drawString("Hai", 100, 100);
            c.update(g);
        } catch (Exception e) {
        }
    }//GEN-LAST:event_doDrawActionPerformed

    private void loadSignedStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_loadSignedStateChanged
        rsLoader = null;
        rsLoader = new RuneClassLoader(loadSigned.getState());
    }//GEN-LAST:event_loadSignedStateChanged

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Thread th = null;
        if (th == null) {

            CanvasPainter cp = new CanvasPainter(this);
            this.g = cp.getGraphics();
            th = new Thread(cp);
            th.start();

            try {
                Thread.sleep(50);
            } catch (Exception e) {
            }

        } else {
            th.stop();
        }

    }//GEN-LAST:event_jButton1ActionPerformed

    private void loadScriptsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadScriptsActionPerformed
    }//GEN-LAST:event_loadScriptsActionPerformed

    public Graphics getGfx() {
        return this.g;
    }

    public Reflection getObjectValue(String name) {
        for (Reflection r : hooks) {
            if (r.getName().equals(name)) {
                return r;
            }
        }
        return null;
    }

    public void loadAllHooks() throws Exception {
        BufferedReader in = new BufferedReader(new FileReader("hooks.txt"));
        String blah = null;
        int loadedHookCount = 0;
        while ((blah = in.readLine()) != null) {
            String data[] = blah.split(" ");
            //System.out.println(data.length);
            if (data.length == 3) {
                hooks.add(new Reflection(this, data[0], data[1], data[2]));
                loadedHookCount++;
            }
        }
        echo.echoSystem("Loaded " + loadedHookCount + " hooks");

    }
    public Console console;
    public RuneClassLoader rsLoader;
    public Keyboard keyboard;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane clientPane;
    private javax.swing.JScrollPane consolePane;
    private javax.swing.JMenuItem doDraw;
    private javax.swing.JButton jButton1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem loadRS;
    private javax.swing.JMenuItem loadScripts;
    private javax.swing.JCheckBoxMenuItem loadSigned;
    private javax.swing.JMenuItem loginIndex;
    private javax.swing.JMenuBar menu;
    private javax.swing.JMenu mnBotting;
    private javax.swing.JMenu mnDebug;
    private javax.swing.JMenu mnScripts;
    private javax.swing.JMenu mnSettings;
    private javax.swing.JCheckBoxMenuItem rRandoms;
    // End of variables declaration//GEN-END:variables
}
